/* eslint-disable vue/attributes-order */
<template>
  <div class="employees">
    <el-card>
      <page-tools :show-left="false">
        <template #right>
          <div>
            <el-button v-setButtonPower="'qqq'" type="primary" @click="goUpload">导入</el-button>
            <el-button type="primary" @click="addClick">+新增员工</el-button>
          </div>
        </template>
      </page-tools>
    </el-card>
    <el-card style="margin-top: 20px">
      <el-table :data="listarr">
        <el-table-column label="序号" type="index">
          <template v-slot="{ $index }">
            <div>
              {{ (page.page - 1) * page.size + $index +1 }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="姓名" prop="username" />
        <el-table-column label="头像">
          <template v-slot="{ row }">
            <img v-imgError="defaultImg" :src="row.staffPhoto" alt="" class="avater" @click="imgClick(row.staffPhoto)">
          </template>
        </el-table-column>
        <el-table-column label="手机号" prop="mobile" />
        <el-table-column label="工号" prop="workNumber" />
        <!--  -->
        <el-table-column label="聘用形式" prop="formOfEmployment" :formatter="formatter">
          <template v-slot="{ row }">
            <div>
              {{ row.formOfEmployment | formatterFormOf }}
            </div>
          </template>
        </el-table-column>
        <!--  -->
        <el-table-column label="部门" prop="departmentName" />
        <el-table-column label="入职时间" prop="correctionTime" :formatter="forDayjs" />
        <el-table-column label="操作">
          <template v-slot="{ row }">
            <div>
              <el-button type="text" @click="look(row)">查看</el-button>
              <el-button type="text">转正</el-button>
              <el-button type="text">调岗</el-button>
              <el-button type="text">离职</el-button>
              <el-button type="text" @click="btnRole(row)">角色</el-button>
              <el-button type="text" @click="del(row)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!-- 尾部分页 -->
      <div class="page">
        <el-pagination
          :current-page="page.page"
          :page-sizes="[1, 2, 3, 5, 10]"
          :page-size="page.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
    <add ref="add" @getData="getData" />
    <!-- 二维码图像 -->
    <el-dialog :visible.sync="isShow" title="生成二维码">
      <div style="text-align= center">
        <canvas ref="canvas" />
      </div>
    </el-dialog>
    <!-- 角色分配 -->
    <role ref="role" v-model="isShowRole" />
  </div>
</template>

<script>
// 导入员工列表
import { sysUser, sysuserDelete } from '@/api/employees.js'
import employeesData from '@/api/constant/employees.js'
// 导入弹框
import add from './components/add.vue'
import dayjs from 'dayjs'
// 导入二维码插件
import Qrcode from 'qrcode'
// 角色导入
import role from './components/role.vue'
export default {
  components: { add, role },
  data() {
    return {
      defaultImg: require('@/assets/common/head.jpg'),
      listarr: [],
      isShowRole: false,
      isShow: false,
      page: {
        page: 10,
        size: 4,
        total: 10
      }
    }
  },
  // 初始化
  created() {
    this.getData()
  },
  methods: {
    handleSizeChange(size) {
      // console.log(`每页 ${size} 条`)
      this.page.size = size
      this.page.page = 1
      this.getData()
    },
    handleCurrentChange(page) {
      // console.log(`当前页: ${page}`)
      this.page.page = page
      this.getData()
    },
    async getData() {
      const res = await sysUser(this.page)
      console.log(res, '列表员工')
      this.listarr = res.data.rows
      this.page.total = res.data.total
      // 获取数据,渲染页面
    },
    formatter(row, column, cellValue, index) {
      const temp = employeesData.hireType.find((item) => {
        return item.id === cellValue
      })
      return temp ? temp.value : '-'
    },
    forDayjs(row, columen, cellValue, index) {
      console.log(cellValue, 'pp')
      return dayjs(cellValue).format('YYYY-MM-DD')
    },
    addClick() {
      // 获取组件上的父元素属性,isshow
      this.$refs.add.isShow = true
    },
    del(row) {
      this.$confirm('你确定要删除吗?', '提示:')
        .then(async() => {
          await sysuserDelete(row.id)
          this.$message.success('删除成功')
          this.getData()
        })
    },
    // 点击导入,通过路由传值,(中文解码编译)
    goUpload() {
      this.$router.push('/employees/upload?name=' + encodeURIComponent('员工'))
    },
    // 点击查看,弹出列表
    look(row) {
      // console.log(row, 3333)
      // 跳转到对应页面
      this.$router.push('employees/detail/' + row.id)
    },
    imgClick(imgS) {
      this.isShow = true
      if (imgS) {
        this.$nextTick(() => {
          Qrcode.toCanvas(this.$refs.canvas, imgS, { width: 200, heigth: 200 })
        })
      } else {
        return
      }
    },
    // 角色
    btnRole(row) {
      this.isShowRole = true
      this.$refs.role.getUserInfo(row.id)
    }
  }

}
</script>

<style lang="scss" scoped>
.employees{
  padding: 15px;
  .page{
    margin-top: 20px;
    text-align: right;
  }
  .avater{
    width: 40px;
    height: 40px;
    // border-radius:50%;
    object-fit: contain;
  }
}
</style>
